package com.miniapp.talk.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper;
import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.ImmersionOwner;
import com.gyf.immersionbar.components.ImmersionProxy;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.miniapp.talk.R;
import com.miniapp.talk.activity.MainActivity;
import com.miniapp.talk.activity.SearchHisActivity;
import com.miniapp.talk.activity.my.MyPersonalCenterActivity;
import com.miniapp.talk.activity.my.MyPersonalCenterTwoActivity;
import com.miniapp.talk.activity.room.CollectionRoomListActivity;
import com.miniapp.talk.activity.room.RankActivity;
import com.miniapp.talk.adapter.MainHomeBestRoomAdapter;
import com.miniapp.talk.adapter.MainHomeBottomLineAdapter;
import com.miniapp.talk.adapter.MainHomeCenterBannerAdapter_1;
import com.miniapp.talk.adapter.MainHomeRecAdapter_1;
import com.miniapp.talk.adapter.MainHomeRoomAdapter_1;
import com.miniapp.talk.adapter.MainHomeTitleAdapter_1;
import com.miniapp.talk.adapter.MainHomeTitleAdapter_2;
import com.miniapp.talk.adapter.MainHomeTitleNewPeopleAdapter;
import com.miniapp.talk.app.utils.RxUtils;
import com.miniapp.talk.base.BaseWebActivity;
import com.miniapp.talk.base.MyBaseArmFragment;
import com.miniapp.talk.base.UserManager;
import com.miniapp.talk.bean.BannerBean;
import com.miniapp.talk.bean.BaseBean;
import com.miniapp.talk.bean.BestRoomResult;
import com.miniapp.talk.bean.RecommendUser;
import com.miniapp.talk.bean.RoomListResult;
import com.miniapp.talk.bean.RoomSimpleIntro;
import com.miniapp.talk.bean.RoomTypeResult;
import com.miniapp.talk.bean.TodayRecommBean;
import com.miniapp.talk.di.CommonModule;
import com.miniapp.talk.di.DaggerCommonComponent;
import com.miniapp.talk.service.CommonModel;
import com.miniapp.talk.view.GlideImageLoader;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * 作者:sgm
 * 描述:首页
 */
public class MainHomeFragment extends MyBaseArmFragment implements ImmersionOwner {

    @Inject
    CommonModel commonModel;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.imgSearch)
    ImageView imgSearch;
    @BindView(R.id.shoucang_room)
    ImageView shoucangRoom;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.sousuo)
    LinearLayout sousuo;
    @BindView(R.id.view_main_bar)
    View viewMainBar;
    @BindView(R.id.rcv_home)
    RecyclerView mRecyclerView;
    @BindView(R.id.layout_point)
    LinearLayout mLayoutBannerIndicator;

    MainActivity mMainActivity;

    DelegateAdapter mDelegateAdapter;

    MainHomeTitleAdapter_1 mMainHomeTitleAdapter_1;//个人推荐title

    MainHomeBestRoomAdapter mBestUnionAdapter;//最佳公会数据

    List<BestRoomResult.DataBean> mBestUnionList = new ArrayList<>();//最佳公会数据

    MainHomeCenterBannerAdapter_1 mCenterBannerAdapter;//中央的轮播图

    List<BannerBean> mCenterBannerList = new ArrayList<>();//中央的轮播图

    MainHomeRecAdapter_1 mNewPeopleAdapter;//新秀推荐

    List<RecommendUser> mNewPeopleList = new ArrayList<>();//新秀推荐

    MainHomeRoomAdapter_1 mRoomRecomendAdapter;//推荐房间

    List<RoomSimpleIntro> mRoomSimpleIntroList = new ArrayList<>();//推荐房间数据


    List<RoomTypeResult.DataBean> mServerList = new ArrayList<>();//房间类型，主要为了后面异步请求完数据后重新排序

    Map<String, List<RoomSimpleIntro>> mSortMap = Collections.synchronizedMap(new HashMap<>());

    Map<String,DelegateAdapter.Adapter> mAdapterMap = Collections.synchronizedMap(new HashMap<>());//房间类型adapter

    int mRoomTypeSize = 0;

    int mCompleteLoadRoomSize = 0;

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCommonComponent.builder()
                .appComponent(appComponent)
                .commonModule(new CommonModule())
                .build()
                .inject(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMainActivity = (MainActivity) context;
    }

    @Override
    public View getLayoutView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return ArmsUtils.inflate(getActivity(), R.layout.fragment_main_home_3);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        initRecyclerView();

        //设置viewpager的页面刷新
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            refreshData();
        });

        refreshLayout.setEnableLoadMore(false);

        imgSearch.setOnClickListener(v -> {
            ArmsUtils.startActivity(RankActivity.class);
        });

        sousuo.setOnClickListener(v -> {
            ArmsUtils.startActivity(SearchHisActivity.class);
        });

//        PeopleRecAdatper peopleRecAdatper = new PeopleRecAdatper(new LinearLayoutHelper(), mMainActivity, "个人推荐", R.mipmap.icon_title_recommend, false);
//
//        mAdapters.add(peopleRecAdatper);

        loadBanner();

        //今日推荐
        loadTodayRecommend();

    }

    //最佳公会
    private void initBestUnionAdapters() {

        RoomTypeResult.DataBean dataBean = new RoomTypeResult.DataBean();
        dataBean.setId("-8888");
        dataBean.setName("最佳公会");

        List<RoomTypeResult.DataBean> titleList = new ArrayList<>();

        titleList.add(dataBean);

        MainHomeTitleAdapter_2 mainTitle2 = new MainHomeTitleAdapter_2(R.layout.item_today_recommend_title, titleList, new SingleLayoutHelper());

        mDelegateAdapter.addAdapter(mainTitle2);

        GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(3);

//        gridLayoutHelper.setHGap(QMUIDisplayHelper.dpToPx(8));

        gridLayoutHelper.setGap(QMUIDisplayHelper.dpToPx(10));

        gridLayoutHelper.setAutoExpand(false);

        mBestUnionAdapter = new MainHomeBestRoomAdapter(mMainActivity, R.layout.item_best_union, mBestUnionList, gridLayoutHelper);

        mBestUnionAdapter.setOnClickListener(new MainHomeBestRoomAdapter.OnChildViewClickListener() {
            @Override
            public void OnChildClick(View view, BestRoomResult.DataBean recommendUser) {
                switch (view.getId()) {
                    case R.id.img_people_head:
                        enterData(recommendUser.getUid(), "", commonModel, 1, recommendUser.getRoom_cover());
                        break;

                }
            }
        });

        mDelegateAdapter.addAdapter(mBestUnionAdapter);

        //中央banner
        mCenterBannerAdapter = new MainHomeCenterBannerAdapter_1(mMainActivity, R.layout.item_home_center_banner, mCenterBannerList, new SingleLayoutHelper());

        mDelegateAdapter.addAdapter(mCenterBannerAdapter);

        loadBestUnions();

        loadCenterBanner();
    }

    private void initRecyclerView() {

        VirtualLayoutManager layoutManager = new VirtualLayoutManager(mMainActivity);

        mRecyclerView.setLayoutManager(layoutManager);

        //设置服用池设置回收复用池大小，（如果一屏内相同类型的 View 个数比较多，需要设置一个合适的大小，防止来回滚动时重新创建 View）：
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

        viewPool.setMaxRecycledViews(0, 10);

        mRecyclerView.setRecycledViewPool(viewPool);

        //创建delegateadapter
        mDelegateAdapter = new DelegateAdapter(layoutManager, false);

//        mRecyclerView.addItemDecoration(new HomeRecommendItemDecoration(mDataList));

//        mMainHomeAdapter = new MainHomeAdapter(mMainActivity, mDataList);

        mRecyclerView.setAdapter(mDelegateAdapter);

//        mDelegateAdapter.setAdapters(mAdapters);

    }

    /**
     * 最佳公会
     */
    private void loadBestUnions() {
        RxUtils.loading(commonModel.good_room_three(), this)
                .subscribe(new ErrorHandleSubscriber<BestRoomResult>(mErrorHandler) {
                    @Override
                    public void onNext(BestRoomResult todayRecommBean) {

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<BestRoomResult.DataBean> recommendUserList = todayRecommBean.getData();

                            if (recommendUserList.size() > 0) {

                                mBestUnionList.clear();

                                mBestUnionList.addAll(recommendUserList);

                                if (mBestUnionAdapter != null) {
                                    mBestUnionAdapter.notifyDataSetChanged();
                                }

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                    }
                });
    }

    /**
     * 中央的banner
     */
    private void loadCenterBanner() {
        RxUtils.loading(commonModel.active_list_three(), this)
                .subscribe(new ErrorHandleSubscriber<BannerBean>(mErrorHandler) {
                    @Override
                    public void onNext(BannerBean bannerBean) {

                        List<BannerBean.DataBean> data = bannerBean.getData();

                        if (data.size() > 0) {

                            mCenterBannerList.clear();

                            mCenterBannerList.add(bannerBean);

                            if (mCenterBannerAdapter != null) {
                                mCenterBannerAdapter.notifyDataSetChanged();
                            }

                        } else {
                            mCenterBannerList.clear();
                            if (mCenterBannerAdapter != null) {
                                mCenterBannerAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                });

    }

    /**
     *
     */
    private void refreshData() {
        //C位推荐
        RxUtils.loading(commonModel.tj_user_three(), this)
                .subscribe(new ErrorHandleSubscriber<TodayRecommBean>(mErrorHandler) {
                    @Override
                    public void onNext(TodayRecommBean todayRecommBean) {
                        refreshLayout.finishRefresh();

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RecommendUser> recommendUserList = todayRecommBean.getData();

                            if (recommendUserList.size() > 0) {


                                if (mDelegateAdapter.getAdaptersCount() >= 2) {

                                    MainHomeRecAdapter_1 mainHomeRecAdapter_11 = (MainHomeRecAdapter_1) mDelegateAdapter.findAdapterByIndex(1);

                                    List<RecommendUser> list = mainHomeRecAdapter_11.getData();
                                    list.clear();
                                    list.addAll(recommendUserList);
                                    mainHomeRecAdapter_11.notifyDataSetChanged();
                                }

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        refreshLayout.finishRefresh();
                    }
                });

        //房间推荐
        RxUtils.loading(commonModel.tuijian_room_three("4"), this)
                .subscribe(new ErrorHandleSubscriber<RoomListResult>(mErrorHandler) {
                    @Override
                    public void onNext(RoomListResult todayRecommBean) {
                        refreshLayout.finishRefresh();

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RoomSimpleIntro> roomList = todayRecommBean.getData();

                            if (roomList.size() > 0) {

                                mRoomSimpleIntroList.clear();

                                mRoomSimpleIntroList.addAll(roomList);

                                if (mRoomRecomendAdapter != null) {
                                    mRoomRecomendAdapter.notifyDataSetChanged();
                                }

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        refreshLayout.finishRefresh();
                    }
                });

        //最佳公会
        loadBestUnions();
        //中央banner
        loadCenterBanner();
        //房间数据
        refreshRoomList();
    }

    //刷新三个类型的房间数据
    private void refreshRoomList() {
//        int mSize = mSortMap.size();
//        mCompleteLoadRoomSize =0;
        if(mSortMap != null && mSortMap.size()>0 && mAdapterMap.size() == mSortMap.size()){//请求过数据且正确的加载到了界面上
            for (String dataBeanKey : mSortMap.keySet()) {//房间类型，获取房间数据

                RxUtils.loading(commonModel.room_list_three(dataBeanKey, "4"), this)
                        .subscribe(new ErrorHandleSubscriber<RoomListResult>(mErrorHandler) {
                            @Override
                            public void onNext(RoomListResult todayRecommBean) {

                                synchronized (this) {

                                    if (todayRecommBean != null && todayRecommBean.getData() != null) {

                                        List<RoomSimpleIntro> listRoom = todayRecommBean.getData();

                                        if (listRoom.size() > 0) {

                                            mSortMap.put(dataBeanKey, listRoom);//t放进去

                                            MainHomeRoomAdapter_1 adapter_1 = (MainHomeRoomAdapter_1) mAdapterMap.get(dataBeanKey);

                                            List<RoomSimpleIntro> adapterDataList = adapter_1.getData();
                                            adapterDataList.clear();
                                            adapterDataList.addAll(listRoom);
                                            adapter_1.notifyDataSetChanged();

//                                            mCompleteLoadRoomSize++;

//                                            if (mCompleteLoadRoomSize == mSize) {//加载完了
//
//                                                for(String key:mAdapterMap.keySet()){
//                                                    LogUtils.debugInfo("刷新房间数据了====");
//
//                                                    MainHomeRoomAdapter_1 adapter_1 = (MainHomeRoomAdapter_1) mAdapterMap.get(key);
//
//                                                    List<RoomSimpleIntro> adapterDataList = adapter_1.getData();
//                                                    adapterDataList.addAll(listRoom);
//                                                    adapter_1.notifyDataSetChanged();
//                                                }
//
//                                            }

                                        }

                                    }
                                }

                            }

                            @Override
                            public void onError(Throwable t) {
                                super.onError(t);

                            }
                        });

            }
        }
    }

    /**
     * C位推荐
     */
    private void loadTodayRecommend() {
        RxUtils.loading(commonModel.tj_user_three(), this)
                .subscribe(new ErrorHandleSubscriber<TodayRecommBean>(mErrorHandler) {
                    @Override
                    public void onNext(TodayRecommBean todayRecommBean) {

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RecommendUser> recommendUserList = todayRecommBean.getData();

                            if (recommendUserList.size() > 0) {

                                List<String> titleList = new ArrayList<>();

                                titleList.add("C位推荐");

                                mMainHomeTitleAdapter_1 = new MainHomeTitleAdapter_1(R.layout.item_today_recommend_title, titleList, new SingleLayoutHelper());

                                mDelegateAdapter.addAdapter(mMainHomeTitleAdapter_1);


                                GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(3);

//                                gridLayoutHelper.setAspectRatio(3);

//                                gridLayoutHelper.setHGap(QMUIDisplayHelper.dpToPx(8));

                                gridLayoutHelper.setGap(QMUIDisplayHelper.dpToPx(10));

                                gridLayoutHelper.setAutoExpand(false);

                                MainHomeRecAdapter_1 mainHomeRecAdapter_1 = new MainHomeRecAdapter_1(mMainActivity, R.layout.item_people_head, recommendUserList, gridLayoutHelper);

                                mainHomeRecAdapter_1.setOnClickListener(new MainHomeRecAdapter_1.OnChildViewClickListener() {
                                    @Override
                                    public void OnChildClick(View view, RecommendUser recommendUser) {

                                        switch (view.getId()) {
                                            case R.id.tv_to_attention://关注人
                                                setFollow(recommendUser);
                                                recommendUser.setIs_follow(1);
                                                mainHomeRecAdapter_1.notifyDataSetChanged();
                                                break;
                                            case R.id.img_people_head:
                                                Intent intent1 = new Intent(getContext(), MyPersonalCenterTwoActivity.class);
                                                String mineId = UserManager.getUser().getUserId() + "";
                                                if (recommendUser.getId().equals(mineId)) {
                                                    intent1.putExtra("sign", 0);
                                                    intent1.putExtra("id", "");
                                                } else {
                                                    intent1.putExtra("sign", 1);
                                                    intent1.putExtra("id", recommendUser.getId());
                                                }
                                                ArmsUtils.startActivity(intent1);
                                                break;

                                        }

                                    }
                                });

                                mDelegateAdapter.addAdapter(1, mainHomeRecAdapter_1);

                                //房间推荐
                                loadRoomRecommendList();

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                    }
                });
    }

    //关注
    private void setFollow(RecommendUser recommendUser) {
        RxUtils.loading(commonModel.follow(String.valueOf(UserManager.getUser().getUserId()), recommendUser.getId() + ""), this)
                .subscribe(new ErrorHandleSubscriber<BaseBean>(mErrorHandler) {
                    @Override
                    public void onNext(BaseBean baseBean) {
//                        showToast(baseBean.getMessage());
                        Toast.makeText(mMainActivity, "已关注", Toast.LENGTH_LONG).show();

                    }
                });
    }

    /**
     * 房间类型
     */
    private void loadRoomType() {
        RxUtils.loading(commonModel.room_type_three(), this)
                .subscribe(new ErrorHandleSubscriber<RoomTypeResult>(mErrorHandler) {
                    @Override
                    public void onNext(RoomTypeResult todayRecommBean) {

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RoomTypeResult.DataBean> recommendUserList = todayRecommBean.getData();

                            mServerList = recommendUserList;

                            mRoomTypeSize = recommendUserList.size();

                            if (recommendUserList.size() > 0) {

                                for (RoomTypeResult.DataBean dataBean : recommendUserList) {//房间类型，获取房间数据

                                    loadRoomList(dataBean);

                                }

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                    }
                });
    }

    /**
     * 获取房间推荐
     */
    private void loadRoomRecommendList() {

        RxUtils.loading(commonModel.tuijian_room_three("4"), this)
                .subscribe(new ErrorHandleSubscriber<RoomListResult>(mErrorHandler) {
                    @Override
                    public void onNext(RoomListResult todayRecommBean) {

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RoomSimpleIntro> roomList = todayRecommBean.getData();

                            if (roomList.size() > 0) {

                                RoomTypeResult.DataBean dataBean = new RoomTypeResult.DataBean();
                                dataBean.setId("-9999");
                                dataBean.setName("房间推荐");

                                List<RoomTypeResult.DataBean> titleList = new ArrayList<>();

                                titleList.add(dataBean);

                                MainHomeTitleAdapter_2 homeTitleAdapter_1 = new MainHomeTitleAdapter_2(R.layout.item_today_recommend_title, titleList, new SingleLayoutHelper());

                                mDelegateAdapter.addAdapter(homeTitleAdapter_1);

                                StaggeredGridLayoutHelper staggeredGridLayoutHelper = new StaggeredGridLayoutHelper(2);
//                                staggeredGridLayoutHelper.setHGap(QMUIDisplayHelper.dpToPx(8));
                                staggeredGridLayoutHelper.setGap(QMUIDisplayHelper.dpToPx(10));

                                int itemCount = mDelegateAdapter.getItemCount();

                                mRoomSimpleIntroList.addAll(roomList);

                                mRoomRecomendAdapter = new MainHomeRoomAdapter_1(mMainActivity, R.layout.item_home_room, mRoomSimpleIntroList, staggeredGridLayoutHelper);

                                mRoomRecomendAdapter.setOnClickListener(new MainHomeRoomAdapter_1.OnChildViewClickListener() {
                                    @Override
                                    public void OnChildClick(View view, RoomSimpleIntro roomSimpleIntro) {
                                        switch (view.getId()) {
                                            case R.id.img_people_head:
                                                enterData(roomSimpleIntro.getUid(), "", commonModel, 1, roomSimpleIntro.getRoom_cover());
                                                break;

                                        }
                                    }
                                });

                                mRoomRecomendAdapter.setStartPosition(itemCount - 1);

                                mRoomRecomendAdapter.setParentType(dataBean);

                                mDelegateAdapter.addAdapter(mRoomRecomendAdapter);

                                //最佳公会
                                initBestUnionAdapters();

                                //房间类型
                                loadRoomType();

                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        //房间类型
                        loadRoomType();
                    }
                });

    }

    /**
     * 获取房间列表
     *
     * @param dataBean
     */
    private void loadRoomList(RoomTypeResult.DataBean dataBean) {

        RxUtils.loading(commonModel.room_list_three(dataBean.getId(), "4"), this)
                .subscribe(new ErrorHandleSubscriber<RoomListResult>(mErrorHandler) {
                    @Override
                    public void onNext(RoomListResult todayRecommBean) {

                        synchronized (this) {

                            if (todayRecommBean != null && todayRecommBean.getData() != null) {

                                List<RoomSimpleIntro> listRoom = todayRecommBean.getData();

                                if (listRoom.size() > 0) {

                                    mSortMap.put(dataBean.getId(), listRoom);//t放进去

                                    mCompleteLoadRoomSize++;

                                    if (mCompleteLoadRoomSize == mRoomTypeSize) {//加载完了
                                        //改变顺序
                                        for (RoomTypeResult.DataBean item : mServerList) {
                                            String id = item.getId();
                                            List<RoomSimpleIntro> roomList = mSortMap.get(id);
                                            List<RoomTypeResult.DataBean> titleList = new ArrayList<>();

                                            titleList.add(item);

                                            MainHomeTitleAdapter_2 homeTitleAdapter_1 = new MainHomeTitleAdapter_2(R.layout.item_today_recommend_title, titleList, new SingleLayoutHelper());


                                            mDelegateAdapter.addAdapter(homeTitleAdapter_1);

                                            StaggeredGridLayoutHelper staggeredGridLayoutHelper = new StaggeredGridLayoutHelper(2);
                                            staggeredGridLayoutHelper.setGap(QMUIDisplayHelper.dpToPx(10));

                                            int itemCount = mDelegateAdapter.getItemCount();

                                            MainHomeRoomAdapter_1 roomAdapter_1 = new MainHomeRoomAdapter_1(mMainActivity, R.layout.item_home_room, roomList, staggeredGridLayoutHelper);

                                            roomAdapter_1.setOnClickListener(new MainHomeRoomAdapter_1.OnChildViewClickListener() {
                                                @Override
                                                public void OnChildClick(View view, RoomSimpleIntro roomSimpleIntro) {
                                                    switch (view.getId()) {
                                                        case R.id.img_people_head:
                                                            enterData(roomSimpleIntro.getUid(), "", commonModel, 1, roomSimpleIntro.getRoom_cover());
                                                            break;

                                                    }
                                                }
                                            });

                                            roomAdapter_1.setStartPosition(itemCount - 1);

                                            roomAdapter_1.setParentType(item);

                                            mDelegateAdapter.addAdapter(roomAdapter_1);
                                            mAdapterMap.put(id, roomAdapter_1);
                                        }

                                        List<String> list = new ArrayList<>();

                                        list.add("2");

                                        MainHomeTitleNewPeopleAdapter peopleAdapter = new MainHomeTitleNewPeopleAdapter(R.layout.item_home_new_people_title, list, new SingleLayoutHelper());

                                        peopleAdapter.setOnClickListener(new View.OnClickListener() {
                                            String selectedId = "2";

                                            @Override
                                            public void onClick(View view) {

                                                switch (view.getId()) {
                                                    case R.id.imgFresh:
                                                        loadNewPeopleRecommend(selectedId);
                                                        break;
                                                    case R.id.textGirl:
                                                        if (view.isSelected()) {
                                                            return;
                                                        }
                                                        selectedId = "2";
                                                        peopleAdapter.getData().set(0, selectedId);
                                                        peopleAdapter.notifyDataSetChanged();
                                                        break;
                                                    case R.id.textBoy:
                                                        if (view.isSelected()) {
                                                            return;
                                                        }
                                                        selectedId = "1";
                                                        peopleAdapter.getData().set(0, selectedId);
                                                        peopleAdapter.notifyDataSetChanged();
                                                        break;
                                                }
                                                loadNewPeopleRecommend(selectedId);
                                            }
                                        });

                                        mDelegateAdapter.addAdapter(peopleAdapter);

                                        //新秀数据
                                        GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(3);

                                        gridLayoutHelper.setGap(QMUIDisplayHelper.dpToPx(10));

                                        gridLayoutHelper.setAutoExpand(false);

                                        mNewPeopleAdapter = new MainHomeRecAdapter_1(mMainActivity, R.layout.item_people_head, mNewPeopleList, gridLayoutHelper);

                                        mNewPeopleAdapter.setOnClickListener(new MainHomeRecAdapter_1.OnChildViewClickListener() {
                                            @Override
                                            public void OnChildClick(View view, RecommendUser recommendUser) {//关注人
                                                switch (view.getId()) {
                                                    case R.id.tv_to_attention://关注人
                                                        setFollow(recommendUser);
                                                        recommendUser.setIs_follow(1);
                                                        mNewPeopleAdapter.notifyDataSetChanged();
                                                        break;
                                                    case R.id.img_people_head:
                                                        Intent intent1 = new Intent(getContext(), MyPersonalCenterTwoActivity.class);
                                                        String mineId = UserManager.getUser().getUserId() + "";
                                                        if (recommendUser.getId().equals(mineId)) {
                                                            intent1.putExtra("sign", 0);
                                                            intent1.putExtra("id", "");
                                                        } else {
                                                            intent1.putExtra("sign", 1);
                                                            intent1.putExtra("id", recommendUser.getId());
                                                        }
                                                        ArmsUtils.startActivity(intent1);
                                                        break;

                                                }
                                            }
                                        });

                                        mDelegateAdapter.addAdapter(mNewPeopleAdapter);

                                        //底线

                                        List<String> lineList = new ArrayList<>();

                                        lineList.add("");

                                        MainHomeBottomLineAdapter bottomLineAdapter = new MainHomeBottomLineAdapter(R.layout.item_home_bottom_string, lineList, new SingleLayoutHelper());

                                        mDelegateAdapter.addAdapter(bottomLineAdapter);

                                        loadNewPeopleRecommend("2");

                                    }

                                }

                            }
                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                    }
                });

    }

    /**
     * 加载新秀推荐
     */
    private void loadNewPeopleRecommend(String sex) {
        RxUtils.loading(commonModel.new_user_three(sex), this)
                .subscribe(new ErrorHandleSubscriber<TodayRecommBean>(mErrorHandler) {
                    @Override
                    public void onNext(TodayRecommBean todayRecommBean) {

                        if (todayRecommBean != null && todayRecommBean.getData() != null) {

                            List<RecommendUser> recommendUserList = todayRecommBean.getData();

                            if (recommendUserList.size() > 0) {

                                mNewPeopleList.clear();

                                mNewPeopleList.addAll(recommendUserList);

                                mNewPeopleAdapter.notifyDataSetChanged();

                            } else {
                                mNewPeopleList.clear();
                                mNewPeopleAdapter.notifyDataSetChanged();
                            }

                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                    }
                });
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    /**
     * 初始化顶部Banner图
     */
    public void loadBanner() {
        RxUtils.loading(commonModel.carousel(""), this)
                .subscribe(new ErrorHandleSubscriber<BannerBean>(mErrorHandler) {
                    @Override
                    public void onNext(BannerBean bannerBean) {
                        List<String> imgurls = new ArrayList<>();
                        List<BannerBean.DataBean> data = bannerBean.getData();
                        for (BannerBean.DataBean list : data) {
                            imgurls.add(list.getImg());
                            View view = LayoutInflater.from(mMainActivity).inflate(R.layout.item_banner_indicator, null);
//                            CheckBox checkBox = view.findViewById(R.id.banner_check_point);
                            mLayoutBannerIndicator.addView(view);
                        }

                        //设置图片加载器
                        banner.setImageLoader(new GlideImageLoader());
                        //设置图片集合
                        banner.setImages(imgurls);
                        //设置指示器位置（当banner模式中有指示器时）
//                        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//                        banner.setIndicatorGravity(BannerConfig.CENTER);
                        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
                        //设置自动轮播，默认为true
                        banner.isAutoPlay(true);
                        banner.setOnBannerListener(position -> {

                            if(bannerBean == null || bannerBean.getData() == null ||TextUtils.isEmpty(bannerBean.getData().get(position).url)){
                                return;
                            }

                            Intent intent = new Intent(getActivity(), BaseWebActivity.class);
                            intent.putExtra("url", bannerBean.getData().get(position).url);
                            intent.putExtra("name", "");
                            ArmsUtils.startActivity(intent);
                        });
                        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int i, float v, int i1) {

                            }

                            @Override
                            public void onPageSelected(int position) {

                                int count = mLayoutBannerIndicator.getChildCount();
                                for (int i = 0; i < count; i++) {
                                    View view = mLayoutBannerIndicator.getChildAt(i);
                                    CheckBox checkBox = view.findViewById(R.id.banner_check_point);
                                    checkBox.setChecked(false);
                                }
                                View view = mLayoutBannerIndicator.getChildAt(position);
                                CheckBox checkBox = view.findViewById(R.id.banner_check_point);
                                checkBox.setChecked(true);

                            }

                            @Override
                            public void onPageScrollStateChanged(int i) {

                            }
                        });
                        //banner设置方法全部调用完毕时最后调用
                        banner.start();
                    }
                });
    }


    @OnClick({R.id.shoucang_room})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shoucang_room:
                ArmsUtils.startActivity(CollectionRoomListActivity.class);
                break;
        }

    }


    /**
     * ImmersionBar代理类
     */
    private ImmersionProxy mImmersionProxy = new ImmersionProxy(this);

    @Override
    public void onLazyBeforeView() {

    }

    @Override
    public void onLazyAfterView() {

    }

    @Override
    public void onVisible() {

    }

    @Override
    public void onInvisible() {

    }

    @Override
    public void initImmersionBar() {

        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .statusBarView(viewMainBar)
                .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init();//设置状态栏白色

    }

    @Override
    public void onResume() {
        super.onResume();
        mImmersionProxy.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mImmersionProxy.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mImmersionProxy.onDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mImmersionProxy.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImmersionProxy.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mImmersionProxy.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mImmersionProxy.onHiddenChanged(hidden);
        if (banner != null) {
            if (hidden) {
                banner.stopAutoPlay();
            } else {
                banner.startAutoPlay();
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mImmersionProxy.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean immersionBarEnabled() {//是否用沉浸式
        return true;
    }
}
